package cn.b.sky.mes.qm.service.impl;

import java.util.List;

import javax.annotation.Resource;

import cn.b.sky.mes.qm.model.DefectLevel;
import cn.b.sky.mes.qm.model.condition.DefectLevelCondition;
import cn.b.sky.mes.qm.service.DefectLevelService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.utils.DatabaseUtils;
import cn.b.sky.i18n.utils.LocaleResourceBundle;
import cn.b.sky.user.SecurityContextUtil;
import cn.b.sky.mes.qm.dao.DefectLevelDao;

@Service("defectLevelService")
public class DefectLevelServiceImpl implements DefectLevelService {
  
  private static final Logger LOG = LoggerFactory.getLogger(DefectLevelServiceImpl.class);

  @Resource
  DefectLevelDao levelDao;

  @Override
  public List<DefectLevel> selectDefectLevelListByCondition(DefectLevelCondition condition) {
    if (condition == null) {
      condition = new DefectLevelCondition();
    }
    if (StringUtils.isBlank(condition.getOrderField())) {
      condition.setOrderField("CreateDateTime");
    }
    return levelDao.selectDefectLevelListByCondition(condition);
  }

  @Override
  public int selectDefectLevelCountByCondition(DefectLevelCondition condition) {
    if (condition == null) {
      condition = new DefectLevelCondition();
    }
    return levelDao.selectDefectLevelCountByCondition(condition);
  }

  @Override
  public DefectLevel getDefectLevel(String siteCode, String defectLevelCode) {
    if (StringUtils.isBlank(siteCode) || StringUtils.isBlank(defectLevelCode)) {
      return null;
    }
    return levelDao.getDefectLevel(siteCode, defectLevelCode);
  }

  @Override
  public void insertDefectLevel(DefectLevel level) {
    if (StringUtils.isBlank(level.getSiteCode()) || StringUtils.isBlank(level.getDefectLevelCode())) {
      return;
    }
    levelDao.insertDefectLevel(level);
  }

  @Override
  public void updateDefectLevel(DefectLevel level) {
    if (StringUtils.isBlank(level.getSiteCode()) || StringUtils.isBlank(level.getDefectLevelCode())) {
      return;
    }
    levelDao.updateDefectLevel(level);

  }

  @Override
  public BatchJobReport deleteDefectLevel(String siteCode, String defectLevelCode) {
    String languageCode = SecurityContextUtil.getLanguageCode();
    if (StringUtils.isBlank(siteCode) || StringUtils.isBlank(defectLevelCode)) {
      return new BatchJobReport("", "siteCode or defectLevelCode is null", false, languageCode, SecurityContextUtil.getSiteCode());
    }
    String message = defectLevelCode;
    boolean success = true;
    String cause = "";

    try {
      levelDao.deleteDefectLevel(siteCode, defectLevelCode);
    } catch (Exception e) {
      LOG.error("{}", e);
      success = false;
      String errorMsg = e.getMessage();
      if (DatabaseUtils.isConstraintsError(errorMsg)) {
        //外键删除已存在提示
        errorMsg = LocaleResourceBundle.getCommonResourceLabel(SecurityContextUtil.getSiteCode(),languageCode, "dataBaseOperationPkFail.M")
            + LocaleResourceBundle.getCommonResourceLabel(SecurityContextUtil.getSiteCode(),languageCode, "notDelete.M");
      }
      cause = errorMsg;
    }
    return new BatchJobReport(message, cause, success, languageCode, SecurityContextUtil.getSiteCode());
  }

  @Override
  public List<DefectLevel> selectDefectLevelListForSuggest(DefectLevelCondition condition) {
    return levelDao.selectDefectLevelListForSuggest(condition);
  }

}
